import { Component } from '@angular/core';
import { NavController, NavParams, AlertController } from 'ionic-angular';
import { Plan } from '../plan/plan'
import { Sign } from '../plan/sign'
import { HabitService } from '../../service/habit.service';
import { DygraphComponent2 } from './signGraph';
@Component({
  selector: 'page-planInfor',
  templateUrl: 'planInfor.html'
})
export class PlanInforPage {
  i : number;
  plan : Plan;
  isEdit : boolean;
  isAdd : boolean;
  planList : Array<Plan>;
  signList : Array<Sign>;
  isRemindingOpen : boolean;
  constructor(public navCtrl: NavController, public navParams : NavParams, public habitService : HabitService, public alertCtrl: AlertController) {
    this.plan = this.navParams.get('plan');
    this.isEdit = this.navParams.get('isEdit');
    this.isAdd = this.navParams.get('isAdd');
    this.isRemindingOpen = false;

    this.signList = [];

  }
  getSign(){
    this.habitService.getSign(this.plan.pID).then(
      signList=>{
      this.signList=signList;
    }
    );
  }
  ngOnInit() : void{
    this.getSign();
  }
  openReminding(){
    this.isRemindingOpen = false;
  }
  closeReminding(){
    this.isRemindingOpen = true;
  }
  edit(){
    this.isEdit = true;
  }
  exitEdit(){
    this.isEdit = false;
  }
  showAlert(str : string) {
    let alert = this.alertCtrl.create({
      subTitle: str,
      buttons: ['确定']
    });
    alert.present();
  }
  update(){
    if(this.plan.signedNum > this.plan.totalNum){
      this.showAlert("总天数不能大于已签到天数！");
    }
    else{
      this.habitService.updatePlan(this.plan).then(
      ()=>{
        this.exitEdit();
      }
    );
  }
}
  add(){
    if(this.plan.pName == null){
      this.showAlert("主题不能为空！");
    }
    else if(this.plan.totalNum == 0){
      this.showAlert("总天数不能为0！");
    }
    else{
       this.habitService.addPlan(this.plan).then(
      ()=>{
        this.habitService.planList.push(this.plan);
        this.exitEdit();
        this.navCtrl.pop();
      }
    );
    }
  }
  sign(){
    this.habitService.sign(this.plan.pID,this.plan.signedNum+1,this.plan.signedNum).then(()=>{this.getSign();this.plan.signedNum++;})
  }


}
